/*******************************************************************************************
This file is part of iToday.

iToday is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iToday is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iToday.  If not, see <http://www.gnu.org/licenses/>.
* ------------------------------------------------------------------------------
*    TimeUtility.cpp - The implement of Time utility
* ------------------------------------------------------------------------------
*    Revision History
*    Date                  Author         Activity ID               Activity Headline
*    2009-10-13            Congqin				none						initial
*******************************************************************************************/

#include "stdafx.h"

TCHAR * Week[] =
{
    TEXT("Sunday,"),
    TEXT("Monday,"),
    TEXT("Tuesday,"),
    TEXT("Wednesday,"),
    TEXT("Thursday,"),
    TEXT("Friday,"),
    TEXT("Starday,")
};

TCHAR * Month[]=
{
    TEXT("December"),
    TEXT("January"),
    TEXT("February"),
    TEXT("March"),
    TEXT("April"),
    TEXT("May"),
    TEXT("June"),
    TEXT("July"),
    TEXT("August"),
    TEXT("September"),
    TEXT("October"),
    TEXT("November")
};


void CTimeManager::DrawTime( HDC hMemoryDC ,HDC hPublicMemoryDC )
{
    LOGFONT lfFont;
    TCHAR Buffer[30];
    TCHAR temp[3];
    int count = 0;

    GetLocalTime( &m_stTime );

    BitBlt( hPublicMemoryDC,
            rtActiveRegion.left,
            rtActiveRegion.top,
            rtActiveRegion.right - rtActiveRegion.left,
            rtActiveRegion.bottom - rtActiveRegion.top,
            hMemoryDC,
            rtActiveRegion.left,
            rtActiveRegion.top + rtHome.top,
            SRCCOPY
          );

    TimeDigitToChar(Buffer, m_stTime.wHour, m_stTime.wMinute );
    lfFont.lfHeight = rtTime.bottom - rtTime.top;
    lfFont.lfWidth = ( rtTime.right - rtTime.left )/5 - 1;
    DrawStringData( hPublicMemoryDC, Buffer, rtTime ,lfFont );

    Buffer[0] = '\0';
    lfFont.lfHeight = (rtData .bottom - rtData.top)*2/3 ;
    lfFont.lfWidth = ( rtData.right - rtData.left )/25 ;
    wcscat( Buffer, Week[ m_stTime.wDayOfWeek % 7 ] );
    wcscat( Buffer, Month[ m_stTime.wMonth % 12 ] );

    if ( m_stTime.wDay / 10 )
        temp[ count++ ] = TOCHAR( m_stTime.wDay / 10 );
    temp[ count++ ] = TOCHAR( m_stTime.wDay % 10 );
    temp[ count ] = '\0';
    wcscat( Buffer, temp );
    DrawStringData( hPublicMemoryDC, Buffer, rtData , lfFont);

}

void  CTimeManager::TimeDigitToChar( TCHAR* Buffer ,WORD wHour,WORD wMinute )
{
    int count = 0;

    if ( wHour/10 )
    {
        Buffer[ count++ ] = TOCHAR( wHour/10 );
    }
    Buffer[ count++ ] = TOCHAR( wHour % 10 );
    Buffer[ count++ ] = ':';

    if ( wMinute/10 )
    {
        Buffer[ count++ ] = TOCHAR( wMinute / 10 );
    }
    else
    {
        Buffer[ count++ ] = '0';
    }
    Buffer[ count++ ] = TOCHAR( wMinute % 10 );
    Buffer[count]	= '\0';

    return ;
}

void CTimeManager::SetData( RECT rt )
{
    rtHome = rt;

    rtActiveRegion.left  = rtTime.left = rtData.left = rtHome.right / 3 ;
    rtActiveRegion.right = rtTime.right = rtData.right = rtHome.right *2/3;

    rtActiveRegion.top  = rtTime.top = rtHome.top +(rtHome.bottom - rtHome.top )/10 ;
    rtData.top = rtTime.bottom = rtHome.top + ((rtHome.bottom - rtHome.top ) *7/10 );
    rtActiveRegion.bottom  = rtData.bottom = rtHome.top + (rtHome.bottom - rtHome.top )*9/10;


    return;
}

char* GetCurrentTime()
{
	SYSTEMTIME currentTime;
	std::string timeString = "";
	char tempBuffer[32];
	
	GetLocalTime(&currentTime);

	//add year
	timeString += _itoa(currentTime.wYear,tempBuffer,10);
	
	//add delimiter
	timeString += "-";
	
	//add month
	if(currentTime.wMonth < 10)
	{
		timeString += "0";
	}
	timeString += _itoa(currentTime.wMonth,tempBuffer,10);

	//add delimiter
	timeString += "-";
	
	//add day
	if(currentTime.wDay < 10)
	{
		timeString += "0";
	}
	timeString += _itoa(currentTime.wDay,tempBuffer,10);

	//add delimiter
	timeString += "T";
	
	//Add hour
	if(currentTime.wHour < 10)
	{
		timeString += "0";
	}
	timeString += _itoa(currentTime.wHour,tempBuffer,10);
	
	//add delimiter
	timeString += ":";

	//add minute
	if(currentTime.wMinute < 10)
	{
		timeString += "0";
	}
	timeString += _itoa(currentTime.wMinute,tempBuffer,10);

	//add delimiter
	timeString += ":";		
	
	//add second
	if(currentTime.wSecond < 10)
	{
		timeString += "0";
	}
	timeString += _itoa(currentTime.wSecond,tempBuffer,10);
	

	//Copies it to a (char *) which is then passed to any function that wants GetCurrentTime
	static char timeStamp[32];
	strcpy(timeStamp, timeString.c_str());

	return timeStamp;
}